//
//  NGSScoreListBoard.m
//  demo_3
//
//  Created by 才 陈 on 12-2-28.
//  Copyright 2012年 sjtu. All rights reserved.
//

#import "NGSScoreListBoard.h"

@implementation NGSScoreListBoard


+(id) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];
    
    /*
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    // Add border for the table
    float startX = (winSize.width-width)/2;
    float startY = (winSize.height-height)/2;
    CGRect bounds = CGRectMake(startX, startY, width, height);
    */
    
    // 'layer' is an autorelease object.
    NGSScoreListBoard *layer = [NGSScoreListBoard node];
    //NGSScoreListBoard *board = [NGSScoreListBoard node];
    
    // add layer as a child to scene
    [scene addChild: layer];
    
    // return the scene
    return scene;
}

-(id) init
{
    if(self=[super initWithColor:ccc4(116, 153, 132, 255) width:POPPING_LAYER_WIDTH height:POPPING_LAYER_HEIGHT])
    {
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        SCREEN_WIDTH = winSize.width;
        SCREEN_HEIGHT = winSize.height;
        
        [self setOpacity:150];
        int poppingScenePosX = (SCREEN_WIDTH-POPPING_LAYER_WIDTH)/2;
        int poppingScenePosY = (SCREEN_HEIGHT-POPPING_LAYER_HEIGHT)/2;
        self.position = CGPointMake(poppingScenePosX, poppingScenePosY);
        self.isTouchEnabled = YES;
        
        int boardItemStartY = POPPING_LAYER_HEIGHT - POPPING_LAYER_PADDING;
        CCLabelTTF *listBoardTitle = [CCLabelTTF labelWithString:@"Score" fontName:@"AmericanTypewriter-Bold" fontSize:SCORE_TITLE_LABEL_HEIGHT];
        [listBoardTitle setAnchorPoint:CGPointMake(0.5, 1)];
        listBoardTitle.position = CGPointMake(POPPING_LAYER_WIDTH/2, boardItemStartY);
        listBoardTitle.color = ccMAGENTA;
        
        [self addChild:listBoardTitle];
        boardItemStartY = boardItemStartY - SCORE_TITLE_LABEL_HEIGHT - SCORE_ITEM_GAP*2;
        
        boardItemStartY = [self addScoreGeneral:boardItemStartY];
        boardItemStartY = [self addScoreList:boardItemStartY];
        boardItemStartY = [self addScoreDetail:boardItemStartY];
        boardItemStartY = [self addComment:boardItemStartY];
        boardItemStartY = [self addMenus:boardItemStartY];
        
    }
    return self;
}

-(int) addScoreGeneral:(int)generalStartY
{
    int startX = POPPING_LAYER_PADDING;
    NSString *totalGeneral = [NSString stringWithFormat:@"%d", [NGSNoodle totalScore]];
    CCLabelTTF *scoreGeneralLabel = [CCLabelTTF labelWithString:totalGeneral fontName:@"AmericanTypewriter-Bold" fontSize:SCORE_TOTAL_FONT_SIZE];
    [scoreGeneralLabel setAnchorPoint:CGPointMake(0, 1)];
    scoreGeneralLabel.position = CGPointMake(startX, generalStartY);
    scoreGeneralLabel.color = ccMAGENTA;
    [self addChild:scoreGeneralLabel];
    
    int startY = generalStartY - (SCORE_TOTAL_FONT_SIZE-SCORE_ITEM_MULTIPLE_SIGN_WIDTH)/2;
    CCSprite *multipleSprite = [CCSprite spriteWithFile:@"count_x_2x.png"];
    [multipleSprite setAnchorPoint:CGPointMake(0, 1)];
    multipleSprite.scale = SCORE_BOARD_BOWL_SCALING;
    multipleSprite.position = CGPointMake(startX+SCORE_TOTAL_FONT_SIZE*                                       5, startY);
    [self addChild:multipleSprite];
    
    return generalStartY - SCORE_TOTAL_FONT_SIZE - SCORE_ITEM_GAP;
}
-(int) addScoreList:(int)listStartY
{
    int itemStartY = listStartY - SCORE_ITEM_HEIGHT - SCORE_ITEM_GAP;
    NSDictionary *dict = [NGSNoodle NOODLE_TYPES];
    for (NGSNoodle *key in [dict allKeys]) {
        NGSNoodle *bowlOfNoodle = [dict objectForKey:key];
        [self addScoreItem:bowlOfNoodle startFrom:itemStartY];
        itemStartY -= SCORE_ITEM_HEIGHT + SCORE_ITEM_GAP*2;
    }
    itemStartY = itemStartY + SCORE_ITEM_HEIGHT;
    boundaryLineStartPoint = CGPointMake(POPPING_LAYER_PADDING, itemStartY);
    boundaryLineEndPoint = CGPointMake(POPPING_LAYER_WIDTH-POPPING_LAYER_PADDING, itemStartY);
    
    return itemStartY;
}
-(void) addScoreItem:(NGSNoodle *)bowlOfNoodle startFrom:(int)posY
{
    CCLayer *beefNoodle = [BowlOfNooldModelLayer beefNoodle:SCORE_BOARD_BOWL_SCALING];
    int scoreItemStartX = POPPING_LAYER_PADDING;
    int scoreItemStartY = posY;
    beefNoodle.anchorPoint = ccp(0, 0);
    beefNoodle.position = CGPointMake(scoreItemStartX, scoreItemStartY);
    [self addChild:beefNoodle];
    
    NoodleType *noodleType = bowlOfNoodle.type;
    CCLabelTTF *scoreItemTitle = [CCLabelTTF labelWithString:noodleType.displayText fontName:@"AmericanTypewriter-Bold" fontSize:SCORE_ITEM_TITLE_FONT_SIZE];
    [scoreItemTitle setAnchorPoint:CGPointMake(0, 0)];
    int itemTitleStartX = scoreItemStartX + SCORE_BOARD_BOWL_WIDTH + SCORE_ITEM_GAP*2;
    int itemTitleStartY = scoreItemStartY + (SCORE_ITEM_HEIGHT-SCORE_ITEM_TITLE_FONT_SIZE)/2;
    scoreItemTitle.position = CGPointMake(itemTitleStartX, itemTitleStartY);
    scoreItemTitle.color = ccMAGENTA;
    [self addChild:scoreItemTitle];
    
    CCSprite *noodleCountMultipleSign = [CCSprite spriteWithFile:@"count_x_2x.png"];
    [noodleCountMultipleSign setScale:SCORE_BOARD_BOWL_SCALING];
    [noodleCountMultipleSign setAnchorPoint:CGPointMake(0, 0)];
    int itemMultipleSignStartX = itemTitleStartX + SCORE_ITEM_TITLE_WIDTH + SCORE_ITEM_GAP*2;
    int itemMultipleSignStartY = scoreItemStartY + (SCORE_ITEM_HEIGHT-SCORE_ITEM_MULTIPLE_SIGN_WIDTH)/2;
    [noodleCountMultipleSign setPosition:CGPointMake(itemMultipleSignStartX, itemMultipleSignStartY)];
    [self addChild:noodleCountMultipleSign];
    
    NSString *count = [NSString stringWithFormat:@"%d", bowlOfNoodle.count];
    CCLabelTTF *noodleCountLabel = [CCLabelTTF labelWithString:count fontName:@"AmericanTypewriter-Bold" fontSize:SCORE_ITEM_TITLE_FONT_SIZE];
    [noodleCountLabel setAnchorPoint:CGPointMake(0, 0)];
    int noodleCountLabelStartX = itemMultipleSignStartX + SCORE_ITEM_MULTIPLE_SIGN_WIDTH + SCORE_ITEM_GAP*2;
    int noodleCountLabelStartY = scoreItemStartY + (SCORE_ITEM_HEIGHT-SCORE_ITEM_TITLE_FONT_SIZE)/2;
    noodleCountLabel.position = CGPointMake(noodleCountLabelStartX, noodleCountLabelStartY);
    noodleCountLabel.color = ccMAGENTA;
    [self addChild:noodleCountLabel];
    
    CCSprite *scoreEqualsSign = [CCSprite spriteWithFile:@"count_x_2x.png"];
    [scoreEqualsSign setScale:SCORE_BOARD_BOWL_SCALING];
    [scoreEqualsSign setAnchorPoint:CGPointMake(0, 0)];
    int scoreEqualsSignStartX = noodleCountLabelStartX + SCORE_ITEM_TITLE_FONT_SIZE + SCORE_ITEM_GAP*2;
    int scoreEqualsSignStartY = scoreItemStartY + (SCORE_ITEM_HEIGHT-SCORE_ITEM_MULTIPLE_SIGN_WIDTH)/2;
    [scoreEqualsSign setPosition:CGPointMake(scoreEqualsSignStartX, scoreEqualsSignStartY)];
    [self addChild:scoreEqualsSign];
    
    NSString *score = [NSString stringWithFormat:@"%d", bowlOfNoodle.count*bowlOfNoodle.type.score];
    CCLabelTTF *scoreLabel = [CCLabelTTF labelWithString:score fontName:@"AmericanTypewriter-Bold" fontSize:SCORE_ITEM_TITLE_FONT_SIZE];
    [scoreLabel setAnchorPoint:CGPointMake(0, 0)];
    int scoreLabelStartX = scoreEqualsSignStartX + SCORE_ITEM_MULTIPLE_SIGN_WIDTH + SCORE_ITEM_GAP*2;
    int scoreLabelStartY = scoreItemStartY + (SCORE_ITEM_HEIGHT-SCORE_ITEM_TITLE_FONT_SIZE)/2;
    scoreLabel.position = CGPointMake(scoreLabelStartX, scoreLabelStartY);
    scoreLabel.color = ccMAGENTA;
    [self addChild:scoreLabel];
    
    
}
-(int) addScoreDetail:(int)detailStartY
{
    ccDrawLine(boundaryLineStartPoint, boundaryLineEndPoint);
    
    detailStartY = detailStartY - SCORE_ITEM_GAP;
    int startX = POPPING_LAYER_PADDING;
    NSString *totalDetail = [NSString stringWithFormat:@"Total:%d", [NGSNoodle totalScore]];
    CCLabelTTF *totalDetailLabel = [CCLabelTTF labelWithString:totalDetail fontName:@"AmericanTypewriter-Bold" fontSize:SCORE_TOTAL_FONT_SIZE];
    [totalDetailLabel setAnchorPoint:CGPointMake(0, 1)];
    totalDetailLabel.position = CGPointMake(startX, detailStartY);
    totalDetailLabel.color = ccMAGENTA;
    [self addChild:totalDetailLabel];
    
    startX = POPPING_LAYER_WIDTH - POPPING_LAYER_PADDING;
    NSString *bestDetail = [NSString stringWithFormat:@"Best:%d", [NGSNoodle maxScore]];
    CCLabelTTF *bestDetailLabel = [CCLabelTTF labelWithString:bestDetail fontName:@"AmericanTypewriter-Bold" fontSize:SCORE_TOTAL_FONT_SIZE];
    [bestDetailLabel setAnchorPoint:CGPointMake(1, 1)];
    bestDetailLabel.position = CGPointMake(startX, detailStartY);
    bestDetailLabel.color = ccMAGENTA;
    [self addChild:bestDetailLabel];
    
    return detailStartY - SCORE_TOTAL_FONT_SIZE - SCORE_ITEM_GAP;
}
-(int) addComment:(int)commentStartY
{
    NSString *comments = [NGSUtils comment:[NGSNoodle totalScore]];
    
    int width = POPPING_LAYER_WIDTH-2*POPPING_LAYER_PADDING;
    UIFont *font = [UIFont fontWithName:@"Thonburi" size:SCORE_LIST_BOARD_COMMENT_FONT_SIZE];
    CGSize labelDimension = [NGSUtils calculateHeightOfText:comments Font:font Width:width BreakMode:UILineBreakModeWordWrap];
    CCLabelTTF *commentLabel  = [CCLabelTTF labelWithString:comments dimensions:labelDimension alignment:CCTextAlignmentCenter fontName:@"Thonburi" fontSize:SCORE_LIST_BOARD_COMMENT_FONT_SIZE];
    [commentLabel setAnchorPoint:CGPointMake(0.5, 1)];
    commentLabel.position = CGPointMake(POPPING_LAYER_WIDTH/2, commentStartY);
    commentLabel.color = ccMAGENTA;
    [self addChild:commentLabel];
    
    return commentStartY - SCORE_LIST_BOARD_COMMENT_FONT_SIZE - SCORE_ITEM_GAP;
}
-(int) addMenus:(int)menuStartY
{
    menuStartY = menuStartY - SCORE_ITEM_GAP;
    NSString *retryButtonImgName = @"sumup_retry_2x.png";
    CCMenuItem *retryMenuItem = [CCMenuItemImage itemFromNormalImage:retryButtonImgName selectedImage:retryButtonImgName target:self selector:@selector(retryButtonTapped:)];
    retryMenu = [CCMenu menuWithItems:retryMenuItem, nil];
    retryMenuItem.anchorPoint = CGPointMake(0.5, 1);
    retryMenuItem.scale = 0.5;
    int retryMenuPosX = (POPPING_LAYER_WIDTH-BUTTON_WIDTH*2)/4;
    retryMenu.position = ccp(retryMenuPosX, menuStartY);

    /*
    retryMenu = [CCSprite spriteWithFile:@"sumup-retry_2x.png"];
    retryMenu.scale = 0.5;
    retryMenu.anchorPoint = CGPointMake(0.5, 1);
    retryMenu.position = ccp(retryMenuPosX, menuStartY);
    */
    
    NSString *quitButtonImgName = @"sumup_quit_2x.png";
    CCMenuItem *quitMenuItem = [CCMenuItemImage itemFromNormalImage:quitButtonImgName selectedImage:quitButtonImgName target:self selector:@selector(quitButtonTapped:)];
    quitMenu = [CCMenu menuWithItems:quitMenuItem, nil];
    quitMenuItem.anchorPoint = CGPointMake(0.5, 1);
    quitMenuItem.scale = 0.5;
    int quitMenuPosX = (3*POPPING_LAYER_WIDTH+BUTTON_WIDTH*2)/4;
    quitMenu.position = ccp(quitMenuPosX, menuStartY);
    
    [self addChild:retryMenu];
    [self addChild:quitMenu];
    
    return 0;
}



-(void) registerWithTouchDispatcher 
{ 
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; 
}

//when the touch operation happen it calls for this function
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{  
    if(self.zOrder==HIDDEN_ITEM_Z_INDEX) return NO;
    else return YES;
}


//when you touch the device and move it calls for this function
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{

}

//when your move finish and your finger leave behind the device it calls for the function
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
}

-(void) retryButtonTapped:(id)sender
{
    if(self.zOrder==POPPING_ITEM_Z_INDEX)
    {
        //CGPoint touchLocation = [touch locationInView: [touch view]];
        //CGRect boundingBox = [self boundingBox];
        //[self.parent reorderChild:self z:HIDDEN_ITEM_Z_INDEX];
        [self setVisible:NO];
    }

}

-(void) quitButtonTapped:(id)sender
{
    if(self.zOrder==POPPING_ITEM_Z_INDEX)
    {
        exit(0);
    }
    
}

-(void) setVisible:(BOOL)visible
{
    if(visible)
    {
        [self.parent reorderChild:self z:POPPING_ITEM_Z_INDEX];
    }
    else
    {
        [self.parent reorderChild:self z:HIDDEN_ITEM_Z_INDEX];
    }
    [super setVisible:visible];
}

/*
-(void)draw
{
    glColor4f(0, 0, 0, 0);  
    glLineWidth(2);
    glEnable(GL_LINE_SMOOTH);
    
    ccDrawLine(boundaryLineStartPoint, boundaryLineEndPoint);
}    
*/


// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	[retryMenu release];
    retryMenu = Nil;
    
    [quitMenu release];
    quitMenu = Nil;
    
	[super dealloc];
}
@end
